class Solution {
public:
    int minPathSum(vector<vector<int>>& grid) {
        int row=grid.size();
        int col=grid[0].size();
        //vector<vector<int>> cost(row,vector<int>(col,0));
        for(int i=row-1;i>=0;i--){
            for(int j=col-1;j>=0;j--){
                if(i==row-1&&j==col-1){
                    //cost[i][j]=grid[i][j];
                    grid[i][j]=grid[i][j];
                } else if(i==row-1&&j!=col-1){
                    //cost[i][j]=grid[i][j]+cost[i][j+1];
                    grid[i][j]=grid[i][j]+grid[i][j+1];
                } else if(j==col-1&&i!=row-1){
                    //cost[i][j]=grid[i][j]+cost[i+1][j];
                    grid[i][j]=grid[i][j]+grid[i+1][j];
                } else{
                    //cost[i][j]=grid[i][j]+(cost[i][j+1]<cost[i+1][j]?cost[i][j+1]:cost[i+1][j]);
                    grid[i][j]=grid[i][j]+(grid[i][j+1]<grid[i+1][j]?grid[i][j+1]:grid[i+1][j]);
                }
                //cout<<cost[i][j]<<" ";
                //cout<<grid[i][j]<<" ";
            }
            //cout<<endl;
        }
        return grid[0][0];
        //return cost[0][0];
    }
};